package tel.tool;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;

import com.alibaba.fastjson.JSON;
import com.sun.javafx.collections.MappingChange.Map;

import tel.tool.model.ResultA;

public class TelSplit {
	public static final String fileUri="E:\\tel\\test.txt";
	public static final String savePath="E:\\tel\\data\\";
	public static final String uri="https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
	public static HashMap<String, String> mtl= new HashMap<String,String>();
	static File erro= new File("./erro.csv");
	static BufferedWriter Errowriter= null;//new BufferedWriter(new FileWriter(erro));;
	static{
		
	}
	public static void main(String[] args) throws ClientProtocolException, IOException {
		try {
			Errowriter =new BufferedWriter(new FileWriter(erro));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		File oldFile = new File(fileUri);
		BufferedReader reader = new BufferedReader(new FileReader(oldFile));
		String tel ="";
		String front ="";
		while ((tel = reader.readLine()) !=null) {
			try{
				
			
			tel =tel.trim();
			front = tel.substring(0, 7);
			File tartget = null;
			if (mtl.containsKey(front)) {
				tartget = new File(savePath+mtl.get(front)+".csv");
				System.out.println(front);
			}else {
				String result = HttpClient.get(uri+tel.trim());
				String json = result.split("=")[1];
//				System.out.println(json);
				ResultA result2 = JSON.parseObject(json,ResultA.class);
				tartget = new File(savePath+result2.getProvince()+".csv");
				mtl.put(front, result2.getProvince());
			}
			if (!tartget.exists()) {
				tartget.createNewFile();
			}
			
			append2FileEnd(tartget, tel);
			}catch (Throwable e) {
				Errowriter.write(tel+"\r\n");
				try {
					Thread.sleep(60);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
		System.err.println("---------------ssss-----end---------ssss-------------");
		File www= new File("./map.csv");
		BufferedWriter writer=null;
		for (String string : mtl.keySet()) {
			writer =new BufferedWriter(new FileWriter(www));
			writer.write(string+","+mtl.get(string)+"\r\n");
		}
		writer.close();
		
	}
	public static void  append2FileEnd(File file ,String line) throws IOException {
		RandomAccessFile randomAccessFile=null;
		try {
			randomAccessFile = new RandomAccessFile(file, "rw");
			randomAccessFile.seek(randomAccessFile.length());
			randomAccessFile.writeBytes(line + "\r\n");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			randomAccessFile.close();
		}
		
	}
}
